import { locInfo , lessonInfo } from '@/api/train'

		import $router from '@/router';
const state = {
  upperDate: '',
  lessonList: locInfo.result.lessonList,
  lessonId: '' ,//课程id
  //日程详情
  lessonInfo:{},
  rollCall:0,
  //扫码签到需要的参数
  infoStartEnd:'',
  params:{qrCode:'',
  coordinate:'',
  status:'CHECK-IN',
  lessonId:''},
   scanCheck:false,
}
const mutations = {
  GET_LESSON_LIST: (state, info) => {
    state.lessonList = info
  },
  GET_LESSON_INFO: (state, info ) => {
	state.rollCall = info.rollCall==='YES'&&(info.lesson.rollCallMethod !== 'ROLL_MANUAL_INPUT')
	state.infoStartEnd = info.lesson.startTime +'-'+ info.lesson.endTime
	let {id:lessonId} = info.lesson,{coordinate,location} = info
	state.params ={coordinate,status:state.params.status,lessonId,location,reason:''}
	//console.log("Qrcode params",info,state.params,qr)
    state.lessonInfo = info.lesson //lessonId=>日程信息
  },
  //是否扫码成功
  SCAN_CHECK: (state, info) => {
    state.scanCheck = info
  },
 
}

const actions = {
	//日程列表
  lessonList({
    commit,
    state
  }, info) {
    console.log(state, info)
    this._vm.$ajax({
      method: 'post',
      url: '/roll_call/query/lesson',
      params: { ...info
      }
    }).then(response => {
      const data = response
      try {
        commit('GET_LESSON_LIST',data.result.lessonList.length?data.result.lessonList:locInfo.result.lessonList)
      } catch (errs) {
		 // alert(errs)
      //  commit('GET_LESSON_LIST', locInfo.result.lessonList)
      }
    }).catch(error => {
      console.log('试试',error)
      commit('GET_LESSON_LIST', locInfo.result.lessonList)
    })
  },
  lessonId(obj){
		return obj
  },
  //日程信息
  lessonInfo({
    commit,
    state
  }, info) {
    console.log(state, info)
    this._vm.$ajax({
      method: 'post',
      url: '/roll_call/query/lesson_info',
      params: { lessonId:info.lessonId
      }
    }).then(response => {
      const data = response
     //console.log('success res',response)
		data.result.coordinate =  info.coordinate
        commit('GET_LESSON_INFO', data.result)
      
    }).catch(error => {
      console.log("12321",error,state,)
     // commit('GET_LESSON_INFO', lessonInfo)
    })
  },
  scanCheck({
    commit,
    state
  }, info) {
    console.log(state, info)
    this._vm.$ajax({
      method: 'post',
      url: '/roll_call/scan/check',
      data: {...info}
     ,
	_transformRequest:true	
    }).then(response => {
		console.log('then',response,this._vm)
		let name = ''
		if(response.status===0){
		try {
			var result = response.result
			name =result.tcName + '/' + result.firstName + result.firstName
		commit('SCAN_CHECK', {...result})
		} catch (err) {
		console.log('err',err)
		// commit('GET_LESSON_INFO', data.result)
		}
		}
		console.log('success scan=====',result)
		//TC.name / EN.name 4269
		debugger
		this._vm.$dialog.alert({message:name+'成功點名!到達時間為' + result.checkTime,showCancelButton:false}).then(()=>{
			//$router.replace({path:'/train/details',query:{lessonId:info.lessonId}})
		})
		
		
    }).catch(error => {
		console.log('error scan=====',error)
		return this._vm.$mesage({type:'warning',message:error.msg})
       // commit('GET_LESSON_INFO', lessonInfo)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
